﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FastShip88.Admin
{
    public partial class AdmUserFee : System.Web.UI.Page
    {
        DBAccess.UsersDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new FastShip88.DBAccess.UsersDataContext();

            lblError.Visible = false;
            lblSuccess.Visible = false;

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string strOutParcelNo = txtOutParcelNo.Text.Trim();
            double tariff = SafeConvert.ToDouble(txtTariff.Text);

            var q_parcel = from p in db.ParcelInfos
                           from q in db.aspnet_Users
                           where p.OutParcelNo.Contains(strOutParcelNo) && p.UserInfo.UserId == q.UserId
                           select new { p.Id, p.UserId, p.TransferReqId, q.UserName };
            if (q_parcel.Count() > 1)
            {
                lblError.Text = "不能通过出站包裹编号唯一确定包裹及用户名";
                lblError.Visible = true;
                return;
            }

            if (q_parcel.Count() < 1)
            {
                lblError.Text = "找不到包裹，请确定输入是否正确";
                lblError.Visible = true;
                return;
            }

            lblUserName.Text = "该包裹属于用户：" + q_parcel.First().UserName;

            if (tariff == 0)
            {
                lblError.Text = "关税金额不正确";
                lblError.Visible = true;
                return;
            }

            int userId = Utility.GetUserId(this);

            var q_bill = new DBAccess.UserBill();
            q_bill.FeeTypeId = 13; //补缴关税
            q_bill.Amount = (decimal) tariff;
            q_bill.ParcelId = q_parcel.First().Id;
            q_bill.IsCleared = false;
            q_bill.IsCancelled = false;
            q_bill.UserId = q_parcel.First().UserId;
            q_bill.OperatorId = userId;
            q_bill.TransReqId = q_parcel.First().TransferReqId;
            q_bill.Date = DateTime.Now;
            q_bill.Memo = txtMemo.Text.Trim();

            db.UserBills.InsertOnSubmit(q_bill);
            db.SubmitChanges();

            lblSuccess.Visible = true;
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtOutParcelNo.Text = "";
            txtTariff.Text = "";
            txtMemo.Text = "";
            lblUserName.Visible = false;
        }
    }
}
